package cn.bonoon.kernel.config;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

import cn.bonoon.kernel.util.StringHelper;

public abstract class ConfigurableBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
	
	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	public static final String ORDINAL_ATTRIBUTE = "ordinal";
	public static final String CLASS_ATTRIBUTE = "class";
	
	protected Element element(Element element, String name) {
		return DomUtils.getChildElementByTagName(element, name);
	}

	protected <T extends Enum<T>> T resolveEnum(Element element, String name, Class<T> enumType){
		String enumValue = element.getAttribute(name);
		try{
			return Enum.valueOf(enumType, enumValue);
		}catch (Exception e) {
			e.printStackTrace();
			//解析不成功，尝试用另一种方式进行解析
		}

		try{
			//尝试使用下标来解析
			return enumType.getEnumConstants()[StringHelper.toint(enumValue)];
		}catch (Exception e) {
			e.printStackTrace();
			//解析不成功，尝试用另一种方式进行解析
		}
		return null;
	}

	/** 读取节点内容，只读取指定名称的节点的内容。如：&lt;ele&gt;文本内容&lt;/ele&gt;，解析的结果只读取文字"文本内容" */
	protected String resolveText(Element element, String name) {
		Element text = DomUtils.getChildElementByTagName(element, name);
		return null == text ? null : text.getTextContent();
	}

	protected List<Element> elements(Element element, String name) {
		return DomUtils.getChildElementsByTagName(element, name);
	}
	
	protected List<Element> elements(Element element, String[] names) {
		return DomUtils.getChildElementsByTagName(element, names);
	}

	public boolean resolveBoolean(Element element, String name) {
		return resolveBoolean(element, name, false);
	}

	public boolean resolveBoolean(Element element, String name, boolean defaultValue) {
		String booleanValue = element.getAttribute(name);
		if (StringHelper.isEmpty(booleanValue)) return defaultValue;
		return Boolean.parseBoolean(booleanValue);
	}
 
	public int resolveInt(Element element, String name) {
		return StringHelper.toint(element.getAttribute(name));
	}
	
	public int resolveOrdinal(Element element){
		return resolveInt(element, ORDINAL_ATTRIBUTE);
	}

	/** 把属性值按","号分隔开，生成数组，如：roles="a,b,c"则会生成数组[a,b,c] */
	public List<String> resolveList(Element element, String name) {
		return StringHelper.toList(element.getAttribute(name));
	}
	
	/** 先读取节点的属性，如果没有该属性的值，再根据名称查找是否有相同名称的节点，如：name="china"和&lt;name&gt;china&lt;/name&gt;是相同的，但会优先从属性中取值 */
	public String resolveValue(Element element, String name){
		String value = element.getAttribute(name);
		if(StringHelper.isEmpty(value)){
			return resolveText(element, name);
		}
		return value;
	}

	/** 取名字为"name"的属性或子节点，如：name="china"和&lt;name&gt;china&lt;/name&gt;是相同的，但会优先从属性中取值 */
	public String resolveName(Element element) {
		return resolveValue(element, NAME_ATTRIBUTE);
	}

	/** 解释ID字段 ;仅从属性中取，如：id="1002" */
	public String resolveId(Element element) {
		return element.getAttribute(ID_ATTRIBUTE);
	}

	/**  */
	public Class<?> resolveClass(Element element, String name, Class<?> defaultClass) {
		String className = element.getAttribute(name);
		if (StringHelper.isNotEmpty(className)) {
			try {
				return Class.forName(className);
			} catch (Throwable t) {
				//找不到class
			}
		}
		return defaultClass;
	}

	/**  */
	public Class<?> resolveClass(Element element, Class<?> defaultClass) {
		return resolveClass(element, CLASS_ATTRIBUTE, defaultClass);
	}
}
